<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/21 0021
 * Time: 16:17
 */

namespace App\Model\Traits;


use App\Model\Comment;
use App\Model\Favorite;
use App\Model\Zan;
use App\User;

trait Common
{


    public function user()
    {
        return $this->belongsTo(User::class);
    }

    //获得所有点赞的用户
//    public function zans()
//    {
//        return $this->morphToMany(User::class, 'zan');
//    }
    public function zans()
    {
        return $this->morphMany(Zan::class, 'zan');
    }

    public function isZan(User $user)
    {
        return $this->zans()->where('user_id', $user['id'])->first();
    }

    public function favorites()
    {
        return $this->morphMany(Favorite::class, 'favorite');
    }

    public function isFavorite(User $user)
    {
        return $this->favorites()->where('user_id', $user['id'])->first();
    }

//关于评论的多态关联
    public function comment()
    {
        return $this->morphMany(Comment::class, 'comment');
    }
}